package com.originskyseed.platform.cms.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONArray;
import com.originskyseed.platform.cms.i18n.CmsResponseMsg;
import com.originskyseed.platform.cms.model.CmsCatalog;
import com.originskyseed.platform.cms.model.CmsContent;
import com.originskyseed.platform.cms.model.CmsSource;
import com.originskyseed.platform.cms.service.ICmsCatalogService;
import com.originskyseed.platform.cms.service.ICmsContentService;
import com.originskyseed.platform.cms.service.ICmsSourceService;
import com.originskyseed.platform.cms.vo.request.CmsContentCatalogRequestVO;
import com.originskyseed.platform.cms.vo.request.CmsContentListRequestVO;
import com.originskyseed.platform.common.annotation.SysLog;
import com.originskyseed.platform.core.aspect.annotation.RequiredParams;
import com.originskyseed.platform.core.base.BaseController;
import com.originskyseed.platform.core.base.BaseLocaleResource;
import com.originskyseed.platform.core.base.BasePage;
import com.originskyseed.platform.core.base.BaseResponseBody;
import com.originskyseed.platform.core.common.code.CommonResponseCode;
import com.originskyseed.platform.core.filter.helper.SingleSignOnTokenHelper;
import com.originskyseed.platform.core.util.bean.RegExpValidatorUtil;
import com.originskyseed.platform.user.model.UserInfo;

/**
 * 内容正文Controller类
 * 
 * @author LiangMingHui
 *
 */
@Controller
@RequestMapping("/cmsContent")
@BaseLocaleResource(localeFileName = "i18n_cms")
public class CmsContentController extends BaseController {

	private static final Logger logger = LoggerFactory.getLogger(CmsContentController.class);

	@DubboReference
	private ICmsContentService cmsContentService;

	@DubboReference
	private ICmsCatalogService cmsCatalogService;

	@DubboReference
	private ICmsSourceService cmsSourceService;

	/**
	 * 消息发布列表页面
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/cmsContentList.html")
	public ModelAndView cmsContentList(HttpServletRequest request, HttpServletResponse response) {
		return new ModelAndView("cms_content/cms_content_list");
	}

	/**
	 * 回收站列表页面
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/cmsRecycleBinList.html")
	public ModelAndView cmsRecycleBinList(HttpServletRequest request, HttpServletResponse response) {
		return new ModelAndView("cms_content/cms_content_recycle_bin_list");
	}

	/**
	 * 分页查询回收站列表
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/cmsRecycleBinList.json")
	@RequiredParams(names = { "pageNo", "pageSize" })
	public BaseResponseBody cmsRecycleBinDataList(HttpServletRequest request) {
		BaseResponseBody result = null;
		try {
			String mallId = null;
			// 请求对象
			CmsContentListRequestVO requestVO = this.getParameterBean(CmsContentListRequestVO.class);
			requestVO.setMallId(mallId);
			requestVO.setIsSys(0);
			BasePage<CmsContent> page = new BasePage<CmsContent>(requestVO.getPageNo(), requestVO.getPageSize());
			page = this.cmsContentService.selectCmsRecycleBinListByPage(requestVO.toMap(), page);
			result = new BaseResponseBody(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), CmsResponseMsg.CMSRECYCLEBIN_LIST_SUCCESS.getMsg()), page);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), CmsResponseMsg.CMSRECYCLEBIN_LIST_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 批量还原已删除信息
	 * 
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/cmsRecycleBinRestore.json")
	@RequiredParams(names = { "ids" })
	@SysLog(module = "cms模块", operationDesc = "批量还原已删除内容正文信息")
	public BaseResponseBody cmsRecycleBinRestore(HttpServletRequest request) {
		BaseResponseBody result = null;
		try {
			// 请求参数
			String[] ids = this.getParameterArray("ids", new String[] {});

			this.cmsContentService.RecycleByPrimaryKeys(Arrays.asList(ids));
			result = new BaseResponseBody(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), CmsResponseMsg.CMSRECYCLEBIN_RECYCLE_SUCCESS.getMsg()), true);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), CmsResponseMsg.CMSRECYCLEBIN_RECYCLE_ERROR.getMsg()), null);
		}
		return result;
	}

	/**
	 * 彻底删除信息
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/cmsRecycleBinDelete.json")
	@RequiredParams(names = { "ids" })
	@SysLog(module = "cms模块", operationDesc = "删除内容正文信息")
	public BaseResponseBody cmsRecycleBinDelete(HttpServletRequest request) {
		BaseResponseBody result = null;
		try {
			// 请求参数
			String[] ids = this.getParameterArray("ids", new String[] {});

			this.cmsContentService.deleteByPrimaryKeys(Arrays.asList(ids));
			result = new BaseResponseBody(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), CmsResponseMsg.CMSRECYCLEBIN_DELETE_SUCCESS.getMsg()), true);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), CmsResponseMsg.CMSRECYCLEBIN_DELETE_ERROR.getMsg()), null);
		}
		return result;
	}

	/**
	 * 加载消息发布树
	 * 
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/cmsContentTree.json")
	public BaseResponseBody cmsContentTree(HttpServletRequest request) {
		BaseResponseBody result = null;
		try {
			String mallId = null;
			Map<String, Object> map = new HashMap<>();
			map.put("isSys", 0);
			map.put("mallId", mallId);
			JSONArray cmsCatalogTree = this.cmsCatalogService.selectCmsCatalogTreeList(map);
			result = new BaseResponseBody(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), CmsResponseMsg.CMSCONTENT_TREE_SUCCESS.getMsg()), cmsCatalogTree);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), CmsResponseMsg.CMSCONTENT_TREE_ERROR.getMsg()), null);
		}
		return result;
	}

	/**
	 * 分页查询信息发布列表
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/cmsContentList.json")
	@RequiredParams(names = { "pageNo", "pageSize" })
	public BaseResponseBody cmsContentList(HttpServletRequest request) {
		BaseResponseBody result = null;
		try {
			String mallId = null;
			// 请求对象
			CmsContentListRequestVO requestVO = this.getParameterBean(CmsContentListRequestVO.class);
			String catalogId = this.getParameterString("catalogId", "");
			requestVO.setIsSys(0);
			Map<String, Object> map = requestVO.toMap();
			String cmsType = requestVO.getCmsType();
			if (StringUtils.isNoneBlank(cmsType)) {
				List<String> array = Arrays.asList(cmsType.split(","));
				List arrayList = new ArrayList(array);
				arrayList.add(catalogId);
				map.put("array", arrayList);
			} else {
				List arrayList = new ArrayList();
				arrayList.add(catalogId);
				map.put("array", arrayList);
			}
			map.put("mallId", mallId);
			BasePage<CmsContent> page = new BasePage<CmsContent>(requestVO.getPageNo(), requestVO.getPageSize());
			page = this.cmsContentService.selectListByPage(map, page);
			result = new BaseResponseBody(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), CmsResponseMsg.CMSCONTENT_LIST_SUCCESS.getMsg()), page);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), CmsResponseMsg.CMSCONTENT_LIST_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 审核列表页面
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/contentAuditList.html")
	public ModelAndView contentAuditList(HttpServletRequest request, HttpServletResponse response) {
		return new ModelAndView("cms_content/cms_content_audit_list");
	}

	/**
	 * 分页查询审核列表
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/contentAuditList.json")
	@RequiredParams(names = { "pageNo", "pageSize" })
	public BaseResponseBody contentAuditList(HttpServletRequest request) {
		BaseResponseBody result = null;
		try {
			String mallId = null;
			// 请求对象
			CmsContentListRequestVO requestVO = this.getParameterBean(CmsContentListRequestVO.class);
			requestVO.setMallId(mallId);
			// 分页查询
			BasePage<CmsContent> page = new BasePage<CmsContent>(requestVO.getPageNo(), requestVO.getPageSize());
			requestVO.setIsSys(0);
			page = this.cmsContentService.selectAuditListByPage(requestVO.toMap(), page);
			result = new BaseResponseBody(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), CmsResponseMsg.CONTENTAUDIT_LIST_SUCCESS.getMsg()), page);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), CmsResponseMsg.CONTENTAUDIT_LIST_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 信息审核
	 * 
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/contentAuditChangeState.json")
	@RequiredParams(names = { "id", "state" })
	@SysLog(module = "cms模块", operationDesc = "审核内容正文信息")
	public BaseResponseBody contentAuditChangeState(HttpServletRequest request) {
		BaseResponseBody result = null;
		try {
			// 请求对象
			CmsContent cmsContent = this.getParameterBean(CmsContent.class);

			UserInfo userInfo = (UserInfo) SingleSignOnTokenHelper.getCurrentUserInfo(UserInfo.class); // 获取当前用户信息
			if (userInfo != null) {
				cmsContent.setPublishCheckUser(userInfo.getName());
			}
			cmsContent.setPublishDate(new Date());
			this.cmsContentService.updateByPrimaryKeySelective(cmsContent);
			result = new BaseResponseBody(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), CmsResponseMsg.CONTENTAUDIT_SUCCESS.getMsg()), true);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), CmsResponseMsg.CONTENTAUDIT_ERROR.getMsg()), null);
		}
		return result;
	}

	/**
	 * 批量更改信息状态为删除
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/cmsContentDelete.json")
	@RequiredParams(names = { "ids" })
	@SysLog(module = "cms模块", operationDesc = "删除内容正文信息")
	public BaseResponseBody cmsContentDelete(HttpServletRequest request) {
		BaseResponseBody result = null;
		try {
			String mallId = null;
			// 请求参数
			String[] ids = this.getParameterArray("ids", new String[] {});
			List<String> idsList = Arrays.asList(ids);
			Map<String, Object> map = new HashMap<>();
			map.put("list", idsList);
			map.put("mallId", mallId);
			boolean flag = false;
			for (String id : idsList) {
				CmsContent cmsContent = this.cmsContentService.selectByPrimaryKey(id);
				if (!"NRZT1".equals(cmsContent.getState()) && !"NRZT8".equals(cmsContent.getState())) {
					flag = true;
				}
			}
			if (flag) {
				result = new BaseResponseBody(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), CmsResponseMsg.DELETE_NODRAFT_ERROR.getMsg()), null);
			} else {
				this.cmsContentService.stateDelByPrimaryKeys(idsList);
				result = new BaseResponseBody(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), CmsResponseMsg.CMSCONTENT_DELETE_SUCCESS.getMsg()), true);
			}
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), CmsResponseMsg.CMSCONTENT_DELETE_ERROR.getMsg()), null);
		}
		return result;
	}

//	/**
//	 * 预览页面
//	 *
//	 * @param request
//	 * @param response
//	 * @return
//	 */
//	@RequestMapping("/contentPreview.html")
//	@RequiredParams(names = { "id" })
//	public ModelAndView contentPreview(HttpServletRequest request, HttpServletResponse response) {
//		Map<String, Object> model = new HashMap<String, Object>();
//		try {
//			// 请求参数
//			String id = this.getParameterString("id", null);
//
//			if (StringUtils.isNotBlank(id)) {
//				CmsContent cmsContent = this.cmsContentService.selectByPrimaryKey(id);
//				CmsSource cmsSource = this.cmsSourceService.selectByPrimaryKey(cmsContent.getInfoSourceId());
//				model.put("cmsContent", cmsContent);
//				model.put("nameCn", cmsSource.getNameCn());
//			}
//		} catch (Exception e) {
//			logger.error(e.getMessage(), e);
//		}
//		return new ModelAndView("cms_content/cms_content_preview", model);
//	}

	/**
	 * 预览页面
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/contentPreview.json")
	@RequiredParams(names = { "id" })
	public BaseResponseBody contentPreview(HttpServletRequest request) {
		BaseResponseBody result = null;
		Map<String, Object> model = new HashMap<String, Object>();
		try {
			// 请求参数
			String id = this.getParameterString("id", null);

			if (StringUtils.isNotBlank(id)) {
				CmsContent cmsContent = this.cmsContentService.selectByPrimaryKey(id);
				CmsSource cmsSource = this.cmsSourceService.selectByPrimaryKey(cmsContent.getInfoSourceId());
				model.put("cmsContent", cmsContent);
				if (cmsSource != null) {
					model.put("nameCn", cmsSource.getNameCn());
				}
			}
			result = new BaseResponseBody(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), CmsResponseMsg.DATA_SUCCESS.getMsg()), model);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), CmsResponseMsg.DATA_ERROR.getMsg()), null);
		}
		return result;
	}

	/**
	 * 置顶
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/cmsContentIsTop.json")
	@RequiredParams(names = { "id" })
	@SysLog(module = "cms模块", operationDesc = "置顶内容正文信息")
	public BaseResponseBody cmsContentIsTop(HttpServletRequest request) {
		BaseResponseBody result = null;
		try {
			// 请求参数
			String id = this.getParameterString("id", null);

			this.cmsContentService.toTop(id);
			result = new BaseResponseBody(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), CmsResponseMsg.CMSCONTENT_ISTOP_SUCCESS.getMsg()), true);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), CmsResponseMsg.CMSCONTENT_ISTOP_ERROR.getMsg()), null);
		}
		return result;
	}

	/**
	 * 取消置顶
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/cmsContentIsNotTop.json")
	@RequiredParams(names = { "id" })
	@SysLog(module = "cms模块", operationDesc = "取消置顶内容正文信息")
	public BaseResponseBody cmsContentIsNotTop(HttpServletRequest request) {
		BaseResponseBody result = null;
		try {
			// 请求参数
			String id = this.getParameterString("id", null);

			this.cmsContentService.toNotTop(id);
			result = new BaseResponseBody(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), CmsResponseMsg.CMSCONTENT_NO_ISTOP_SUCCESS.getMsg()), true);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), CmsResponseMsg.CMSCONTENT_NO_ISTOP_ERROR.getMsg()), null);
		}
		return result;
	}

	/**
	 * 发布或撤销
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/cmsContentPublishOrRevoke.json")
	@RequiredParams(names = { "id" })
	@SysLog(module = "cms模块", operationDesc = "发布或撤销内容正文信息")
	public BaseResponseBody cmsContentPublish(HttpServletRequest request) {
		BaseResponseBody result = null;
		try {
			// 请求参数
			String id = this.getParameterString("id", null);

			CmsContent cmsContent = this.cmsContentService.selectByPrimaryKey(id);
			if ("NRZT3".equals(cmsContent.getState())) {
				cmsContent.setState("NRZT1");
			} else {
				CmsCatalog cmsCatalog = cmsCatalogService.selectByPrimaryKey(cmsContent.getCatalogId());
				if (cmsCatalog.getCheckType() == 2) {
					cmsContent.setState("NRZT3");
				} else {
					cmsContent.setState("NRZT2");
				}
			}
			this.cmsContentService.updateByPrimaryKey(cmsContent);
			result = new BaseResponseBody(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), CmsResponseMsg.CMSCONTENT_PUBLISH_SUCCESS.getMsg()), true);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), CmsResponseMsg.CMSCONTENT_PUBLISH_ERROR.getMsg()), null);
		}
		return result;
	}

	/**
	 * 内容编辑页面
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/cmsContentEdit.html")
	public ModelAndView cmsContentEdit(HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> model = new HashMap<String, Object>();
		try {
			String mallId = null;
			// 请求参数
			String id = this.getParameterString("id", null);
			String catalogId = this.getParameterString("catalogId", null);
			CmsCatalog cmsCatalog = this.cmsCatalogService.selectByPrimaryKey(catalogId);
			CmsCatalog cmsOneLevelCatalog = null;
			CmsCatalog cmsTwoLevelCatalog = null;
			CmsCatalog cmsThreeLevelCatalog = null;
			if (cmsCatalog != null) {
				if (cmsCatalog.getParentId() == null) {
					cmsOneLevelCatalog = cmsCatalog;
				} else {
					CmsCatalog cmsCatalog2 = this.cmsCatalogService.selectByPrimaryKey(cmsCatalog.getParentId());
					if (cmsCatalog2 != null) {
						if (cmsCatalog2.getParentId() == null) {
							cmsOneLevelCatalog = cmsCatalog2;
							cmsTwoLevelCatalog = cmsCatalog;
						} else {
							CmsCatalog cmsCatalog3 = this.cmsCatalogService.selectByPrimaryKey(cmsCatalog2.getParentId());
							cmsOneLevelCatalog = cmsCatalog3;
							cmsTwoLevelCatalog = cmsCatalog2;
							cmsThreeLevelCatalog = cmsCatalog;
						}
					}
				}
			}

			// 修改页面传入参数
			if (StringUtils.isNotBlank(id)) {
				CmsContent cmsContent = this.cmsContentService.selectByPrimaryKey(id);
				model.put("cmsContent", cmsContent);
				cmsCatalog = this.cmsCatalogService.selectByPrimaryKey(cmsContent.getCatalogId());
				CmsSource cmsSource = this.cmsSourceService.selectByPrimaryKey(cmsContent.getInfoSourceId());
				model.put("sourceName", cmsSource == null ? "" : cmsSource.getNameCn());
			}
			model.put("cmsOneLevelCatalog", cmsOneLevelCatalog);
			model.put("cmsTwoLevelCatalog", cmsTwoLevelCatalog);
			model.put("cmsThreeLevelCatalog", cmsThreeLevelCatalog);
			model.put("catalog", cmsCatalog);
			Map<String, Object> map = new HashMap();
			map.put("isSys", 0);
			map.put("mallId", mallId);
			model.put("cmsSourceList", this.cmsSourceService.selectAll(map));
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		return new ModelAndView("cms_content/cms_content_edit", model);
	}

	/**
	 * 内容编辑请求
	 *
	 * @param request
	 * @param response
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/cmsContentEdit.json")
	public BaseResponseBody cmsContentEditJson(HttpServletRequest request, HttpServletResponse response) {
		BaseResponseBody result = null;
		try {
			Map<String, Object> map = new HashMap<>();
			String mallId = null;
			// 请求参数
			String id = this.getParameterString("id", null);
			String catalogId = this.getParameterString("catalogId", null);
			CmsCatalog cmsCatalog = this.cmsCatalogService.selectByPrimaryKey(catalogId);
			CmsCatalog cmsOneLevelCatalog = null;
			CmsCatalog cmsTwoLevelCatalog = null;
			CmsCatalog cmsThreeLevelCatalog = null;
			if (cmsCatalog != null) {
				if (cmsCatalog.getParentId() == null) {
					cmsOneLevelCatalog = cmsCatalog;
				} else {
					CmsCatalog cmsCatalog2 = this.cmsCatalogService.selectByPrimaryKey(cmsCatalog.getParentId());
					if (cmsCatalog2 != null) {
						if (cmsCatalog2.getParentId() == null) {
							cmsOneLevelCatalog = cmsCatalog2;
							cmsTwoLevelCatalog = cmsCatalog;
						} else {
							CmsCatalog cmsCatalog3 = this.cmsCatalogService.selectByPrimaryKey(cmsCatalog2.getParentId());
							cmsOneLevelCatalog = cmsCatalog3;
							cmsTwoLevelCatalog = cmsCatalog2;
							cmsThreeLevelCatalog = cmsCatalog;
						}
					}
				}
			}

			// 修改页面传入参数
			if (StringUtils.isNotBlank(id)) {
				CmsContent cmsContent = this.cmsContentService.selectByPrimaryKey(id);
				map.put("cmsContent", cmsContent);
				cmsCatalog = this.cmsCatalogService.selectByPrimaryKey(cmsContent.getCatalogId());
				CmsSource cmsSource = this.cmsSourceService.selectByPrimaryKey(cmsContent.getInfoSourceId());
				map.put("sourceName", cmsSource == null ? "" : cmsSource.getNameCn());
			}
			map.put("cmsOneLevelCatalog", cmsOneLevelCatalog);
			map.put("cmsTwoLevelCatalog", cmsTwoLevelCatalog);
			map.put("cmsThreeLevelCatalog", cmsThreeLevelCatalog);
			map.put("catalog", cmsCatalog);
			Map<String, Object> map1 = new HashMap();
			map1.put("isSys", 0);
			map1.put("mallId", mallId);
			map.put("cmsSourceList", this.cmsSourceService.selectAll(map1));
			result = new BaseResponseBody(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), CmsResponseMsg.CMS_SOURCE_LIST_SUCCESS.getMsg()), map);
		} catch (Exception e) {
			result = new BaseResponseBody(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), CmsResponseMsg.CMS_SOURCE_LIST_ERROR.getMsg()), null);
			logger.error(e.getMessage(), e);
		}
		return result;
	}

	/**
	 * 保存信息
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/cmsContentSave.json")
	@RequiredParams(names = { "title", "catalogId" })
	@SysLog(module = "cms模块", operationDesc = "保存或修改内容正文信息")
	public BaseResponseBody cmsContentSave(HttpServletRequest request) {
		BaseResponseBody result = null;
		try {
			UserInfo userInfo = SingleSignOnTokenHelper.getCurrentUserInfo(UserInfo.class);
			String mallId = null;
			// 请求对象、请求参数
			CmsContent cmsContent = this.getParameterBean(CmsContent.class);
			String nameCn = this.getParameterString("infoSourceName", null);
			cmsContent.setMallId(mallId);
			if (!RegExpValidatorUtil.isNumber(cmsContent.getPriority() + "")) {
				return new BaseResponseBody(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), CmsResponseMsg.PRIORITY_NOT_INT_NUMBER.getMsg()), null);
			}
			// 传id则代表修改
			if (StringUtils.isNotBlank(cmsContent.getId())) {
				// 判断是否为图片新闻
				if (cmsContent.getPicturePath() != null && !"".equals(cmsContent.getPicturePath())) {
					cmsContent.setIsPicture(1);
				} else {
					cmsContent.setIsPicture(0);
					cmsContent.setPicturePath("");
				}
				this.cmsContentService.updateByPrimaryKeySelective(cmsContent);
			} else {
				CmsSource cmsSource = new CmsSource();
				cmsSource.setNameCn(nameCn == null ? "" : nameCn);
				cmsSource.setIsSys(0);
				cmsSource.setMallId(mallId);
				// 判断来源是否存在，不存在则新增来源
				if (!this.cmsSourceService.existSourceNameCn(cmsSource)) {
					cmsSource.setIsSys(0);
					cmsSource.setMallId(mallId);
					this.cmsSourceService.insertSelective(cmsSource);
				}
				// 判断是否为图片新闻
				if (cmsContent.getPicturePath() != null && !"".equals(cmsContent.getPicturePath())) {
					cmsContent.setIsPicture(1);
				} else {
					cmsContent.setPicturePath("");
					cmsContent.setIsPicture(0);
				}

				// 查询来源id赋值给内容
				Map<String, Object> map = new HashMap<>();
				map.put("nameCn", nameCn);
				map.put("isSys", 0);
				map.put("mallId", mallId);
				if (cmsSource.getNameCn() != null && !cmsSource.getNameCn().equals("")) {
					cmsContent.setInfoSourceId(this.cmsSourceService.selectByNameCn(map).getId());
				}
				cmsContent.setIsSys(0);
				cmsContent.setIsTop(0);
				this.cmsContentService.insertSelective(cmsContent);
			}
			result = new BaseResponseBody(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), CmsResponseMsg.CMS_CONTENT_SAVE_SUCCESS.getMsg()), cmsContent);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), CmsResponseMsg.CMS_CONTENT_SAVE_ERROR.getMsg()), null);
		}
		return result;
	}

	/**
	 * 查询三级目录
	 * 
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/selectThirdLevelCatalog.json")
	@RequiredParams(names = { "id" })
	public Map<String, Object> selectThirdLevelCatalog(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			// 请求参数
			String id = this.getParameterString("id", null);

			List<CmsCatalog> replies = this.cmsCatalogService.selectThirdlevelList(id);
			map.put("replies", replies);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		return map;
	}

	/**
	 * 帮助列表
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/cmsContentPreview.html")
	public ModelAndView cmsContentPreview(HttpServletRequest request, HttpServletResponse response) {
		return new ModelAndView("cms_content/cms_content_help_proview");
	}

	/**
	 * 查询帮助列表数据
	 * 
	 * @param request
	 * @return
	 */

	@ResponseBody
	@RequestMapping("/cmsContentHelpProview.json")
	@RequiredParams(names = { "catalogId" })
	public Map<String, Object> cmsContentHelpProview(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		try { // 请求参数
			String catalogId = this.getParameterString("catalogId", null);

			if (this.cmsCatalogService.selectByParentId(catalogId).size() == 0) {
				List<CmsContent> replies = new ArrayList<CmsContent>();
				replies.addAll(this.cmsContentService.selectByCatalogId(catalogId));
				map.put("replies", replies);
			}
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		return map;
	}

	/**
	 * 查询最新消息
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/selectNewsContent.json")
	@RequiredParams(names = { "cmsCataCode", "pageNo", "pageSize" })
	public BaseResponseBody selectNewsContent(HttpServletRequest request) {
		BaseResponseBody result = null;
		try {
			Map<String, Object> map = new HashMap<>(16);
			// 请求对象
			CmsContentCatalogRequestVO requestVO = this.getParameterBean(CmsContentCatalogRequestVO.class);
			if (!requestVO.getCmsCataCode().equals("newsCatalog")) {
				return new BaseResponseBody(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), CmsResponseMsg.PARAMS_CATA_CODE_VALUE_ERROR.getMsg()), null, null);
			}
			String mallId = null;
			// 查询分类信息
			Map catelogParam = new HashMap(16);
			catelogParam.put("cmsCataCode", requestVO.getCmsCataCode());
			catelogParam.put("mallId", mallId);
			List<CmsCatalog> cmsCatalogList = this.cmsCatalogService.selectCmsCatalogByCmsCataCode(map);
			if (cmsCatalogList.size() > 0) {
				CmsCatalog cmsCatalog = cmsCatalogList.get(0);
				if (ObjectUtils.allNotNull(cmsCatalog)) {
					map.put("nameCn", cmsCatalog.getNameCn());
					// 查询内容信息
					Map contentParam = new HashMap(16);
					contentParam.put("catalogId", cmsCatalog.getId());
					contentParam.put("mallId", mallId);
					BasePage<CmsContent> page = new BasePage(requestVO.getPageNo(), requestVO.getPageSize());
					page = this.cmsContentService.selectNewsInfoByPage(contentParam, page);
					map.put("contentInfo", page.getResults());
					result = new BaseResponseBody(true, CommonResponseCode.SUCCESS.getCode(), null, map);// this.getLocaleMessage(this.getClass(),
					// CmsResponseMsg.NEWS_NEW_INFO_SUCCESS.getMsg())
				}
			}
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody(false, CommonResponseCode.ERROR.getCode(), null, null);// this.getLocaleMessage(this.getClass(),
																									// CmsResponseMsg.NEWS_NEW_INFO_ERROR.getMsg())
		}
		return result;
	}

	/**
	 * 查询最新消息文章，显示到页面
	 */
	@RequestMapping("/cmsContentShow.html")
	public ModelAndView cmsContentShow(HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> map = new HashMap(16);
		String contentId = this.getParameterString("contentId", null);
		try {
			CmsContent cmsContent = this.cmsContentService.selectByPrimaryKey(contentId);
			map.put("cmsContent", cmsContent);
		} catch (Exception e) {
			e.printStackTrace();
//			map.put("cmsContent","显示文章出错了，请查看日志");
		}
		return new ModelAndView("cms_content/cms_content", map);
	}

}
